package com.woniu.web;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sun.deploy.net.HttpResponse;
import com.woniu.pojo.Emp;
import com.woniu.pojo.Tree;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Gateway
 */
@RestController
@RequestMapping("tree")
public class EmpController {
//我加入了
    @RequestMapping("treemenu")
    public List<Tree> showEmpTree(String id) throws IOException {
        File file = new File("D:\\a\\data.json");
        ObjectMapper mapper = new ObjectMapper();
        List<Emp> empList = mapper.readValue(file, new TypeReference<List<Emp>>() {
        });
        List<Tree> trees = new ArrayList<>();
        empList.forEach(emp -> {
            Tree tree = new Tree(emp.getCode(), emp.getName(), "closed", emp.getSuperior_code());
            trees.add(tree);
        });
        List<Tree> menu = new ArrayList<>();
        for (Tree tree : trees) {
            if (id == null) {
                if ("".equals(tree.getFaterId())) {
                    menu.add(tree);
                }
            } else {
                if (id.equals(tree.getFaterId())) {
                    menu.add(tree);
                }
            }
        }
        return menu;
    }

    @RequestMapping("exception")
    public String showException(HttpResponse resp, HttpRequest req) {
        
        Tree tree = null;
        return tree.toString();
    }
}
